package com.edu.ahm.thread;

/**
 * 龟兔赛跑案例  sleep 的使用
 *
 */
public class Thread5 implements Runnable {

    private static String winner;


    @Override
    public void run() {
        for (int i=1;i<=100 ;i++){
            boolean flag = gameover(i);
            if (Thread.currentThread().getName().equals("兔子")&& i%10==0){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");
            if (flag){
                break;
            }
        }
    }
    //判断是否完成比赛
    private boolean gameover(int path){
        if (winner!=null){
            return true;
        }
        if (path==100){
            winner = Thread.currentThread().getName();
            System.out.println("winner is "+winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        new Thread(new Thread5(),"兔子").start();
        new Thread(new Thread5(),"乌龟").start();
    }
}
